/*
    Copyright (c);2019-2022;Wiscom System;

	All rights reserved.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the Wiscom System nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include <stdio.h>
#include "los_swtmr.h"
#include "time.h"
#include "los_sys.h"
#include "los_api_timer.h"
#include "los_inspect_entry.h"
#include "los_swtmr.ph"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cpluscplus */
#endif /* __cpluscplus */

#define CHECK_SWTMRID(usSwTmrID, uvIntSave, usSwTmrCBID, pstSwtmr)\
{\
    if (usSwTmrID >= OS_SWTMR_MAX_TIMERID)\
   {\
       return LOS_ERRNO_SWTMR_ID_INVALID;\
   }\
   uvIntSave = LOS_IntLock();\
   usSwTmrCBID = usSwTmrID % LOSCFG_BASE_CORE_SWTMR_LIMIT;\
   pstSwtmr = m_pstSwtmrCBArray + usSwTmrCBID;\
   if (pstSwtmr->usTimerID != usSwTmrID)\
   {\
       LOS_IntRestore(uvIntSave);\
       return LOS_ERRNO_SWTMR_ID_INVALID;\
   }\
}
static UINT32 g_timercount1 = 0;
static UINT32 g_Tickcount1[2] = {0};
static VOID Timer1_Callback(UINT32 arg)
{
    g_Tickcount1[1]=(UINT32)LOS_TickCountGet();
    g_timercount1++;
    dprintf("\r\n1. Timer1_Callback entry.\r\n");
}

UINT32 Example_TimerOnceStopDel(VOID)
{
    UINT16 id1;
    UINT32 tick_last1;
    UINT32 uwRet = LOS_OK;
    SWTMR_CTRL_S  *pstSwtmr;
    UINTPTR  uvIntSave;
    UINT16 usSwTmrCBID;
    dprintf("\r\n--------4.11 Timer Once StopDel test!--------\r\n");
#if (LOSCFG_BASE_CORE_SWTMR_ALIGN == YES)
   uwRet = LOS_SwtmrCreate(500, LOS_SWTMR_MODE_ONCE, Timer1_Callback, &id1, 1, OS_SWTMR_ROUSES_ALLOW, OS_SWTMR_ALIGN_SENSITIVE);
#else
   uwRet = LOS_SwtmrCreate(500, LOS_SWTMR_MODE_ONCE, Timer1_Callback, &id1, 1);
#endif
   if (LOS_OK != uwRet)
   {
       dprintf("create Timer1 failed .\r\n");
   }
   else
   {
       dprintf("\r\n1. create Timer1 success! uwInterval is 500 ms.\r\n");
   }
    g_timercount1=0;
    g_Tickcount1[0] = (UINT32)LOS_TickCountGet();
    LOS_SwtmrStart(id1);
    CHECK_SWTMRID(id1, uvIntSave, usSwTmrCBID, pstSwtmr);
    dprintf("\r\n1. pstSwtmr.uwCount after start is %d.\r\n",(*pstSwtmr).uwCount);
 
    LOS_TaskDelay(250);
    dprintf("\r\n1. pstSwtmr.uwCount after delay 250ms is %d.\r\n",(*pstSwtmr).uwCount);
    LOS_SwtmrStop(id1);
    dprintf("\r\n1. pstSwtmr.uwCount after stop is %d.\r\n",(*pstSwtmr).uwCount);
    LOS_TaskDelay(250);
    dprintf("\r\n1. pstSwtmr.uwCount after stop  250ms is %d.\r\n",(*pstSwtmr).uwCount);
    LOS_SwtmrDelete(id1);
    dprintf("\r\n1. pstSwtmr.uwCount after delete is %d.\r\n",(*pstSwtmr).uwCount);
    LOS_TaskDelay(250);
    dprintf("\r\n1. pstSwtmr.uwCount after delete 250ms is %d.\r\n",(*pstSwtmr).uwCount);
    return LOS_OK;
}

#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cpluscplus */
#endif /* __cpluscplus */
